﻿Imports System.Xml.Serialization
Imports System.IO
Imports System.Runtime.CompilerServices

Public Module SettingsRW
    <Extension()>
    Public Function ReadSettings(Of T As Class)(ByVal settingFile As String) As T
        Dim Serializer As New Xml.Serialization.XmlSerializer(GetType(T))
        Try
            Using reader = File.OpenRead(settingFile)
                Return Serializer.Deserialize(reader)
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    <Extension()>
    Public Function WriteSettings(Of T As Class)(ByVal setting As T, ByVal settingFile As String) As Boolean
        Dim Serializer As New Xml.Serialization.XmlSerializer(GetType(T))
        Try
            Using writer = File.OpenWrite(settingFile)
                Serializer.Serialize(writer, setting)
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Module
